﻿using AutoMapper;
using PeiYue.Common;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Common;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class TaskDetailServices : BaseServices<TaskDetailDao>, ITaskDetailServices
    {
        IBaseRepository<TaskDetailDao> _dal;
        IImgResDaoServices _imgResDaoServices;
        IMapper _mapper;
        public TaskDetailServices(IBaseRepository<TaskDetailDao> dal, IMapper mapper, IImgResDaoServices imgResDaoServices)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
            this._imgResDaoServices = imgResDaoServices;
        }
        public async Task<PageModel<SearchTaskDetailResponseDto>> GetPage(SearchTaskDetailRequestDto requestDto)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*,u1.namec as create_names,u2.namec as update_names,");
            strSql.Append("s.street, ");
            strSql.Append("s.names shop_name,rs.expand_time ");
            strSql.Append("from oms_tms_task_detail t ");
            strSql.Append("left join oms_tms_task_header h on t.taskid = h.id ");
            strSql.Append("left join oms_ur_user u1 on t.create_user=u1.id ");
            strSql.Append("left join oms_ur_user u2 on t.update_user=u2.id ");
            //strSql.Append("left join oms_crm_customer c on t.customerid=c.id ");
            strSql.Append("left join oms_crm_customer_shop s on t.shopid=s.id ");
            strSql.Append("left join oms_wms_router_shop rs on rs.customer_id=t.customerid and rs.shop_id=t.shopid and rs.router_id=h.lineid ");
            strSql.Append("where rs.status=1 and s.status=1  ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            if (requestDto.task_id > 0)
            {
                strSql.Append("and t.taskid=@taskid ");
                parameters.Add(new SugarParameter("@taskid", requestDto.task_id));
            }
            if (requestDto.taskstatus.HasValue && requestDto.taskstatus > 0)
            {
                strSql.Append("and t.task_status=@task_status ");
                parameters.Add(new SugarParameter("@task_status", requestDto.taskstatus));
            }
            //if (requestDto.customer_id.HasValue && requestDto.customer_id > 0)
            //{
            //    strSql.Append("and t.customerid=@customerid ");
            //    parameters.Add(new SugarParameter("@customerid", requestDto.customer_id));
            //}
            if (requestDto.shop_id.HasValue && requestDto.shop_id > 0)
            {
                strSql.Append("and t.shopid=@shopid ");
                parameters.Add(new SugarParameter("@shopid", requestDto.shop_id));
            }
            if (requestDto.status.HasValue && requestDto.status > 0)
            {
                strSql.Append("and t.status=@status ");
                parameters.Add(new SugarParameter("@status", (int)CommonEnum.Status.Enable));
            }
            else
            {
                strSql.Append("and t.status<3 ");
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timef))
            {
                strSql.Append("and t.create_time>=@create_time_begin ");
                parameters.Add(new SugarParameter("@create_time_begin", requestDto.timef));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timet))
            {
                strSql.Append("and t.create_time<@create_time_end ");
                parameters.Add(new SugarParameter("@create_time_end", Convert.ToDateTime(requestDto.timet).AddDays(1)));
            }
            var list= await base.SqlQueryPage<SearchTaskDetailResponseDto>(strSql.ToString(), parameters, requestDto.pageIndex, requestDto.pageSize, " rs.expand_time ");
            foreach (var item in list.data)
            {
                var imgList = await _imgResDaoServices.Query(t => t.ResId == item.id && t.ImgType == 5 && t.status == (int)CommonEnum.Status.Enable);
                var data = imgList.Select(t => Appsettings.app(new string[] { "FileUrl" }) + t.ImgPath).ToList();
                item.imgs = data;
            }
            return list;
        }
    }
}
